package com.lebang.service.buss;

import com.lebang.domain.MemberInviteRebateRecord;
import com.lebang.entity.LbbDict;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.entity.member.LbbMemberInviteDetail;
import com.lebang.example.LbbMemberInviteDetailExample;
import com.lebang.mapper.LbbMemberInviteDetailMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.mapper.MemberInviteRebateRecordMapper;
import com.lebang.model.MoneyCategory;
import com.lebang.model.Whether;
import com.lebang.model.enums.Booleans;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.MemberInviteRebateRecordService;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.MemberRebateTotalmoneyService;
import com.lebang.service.sys.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Auther: tony
 * @Date: 2019-01-24 14:35
 * @Description:
 */
@Service
public class MemberInviteDetailService {

    @Autowired
    private MemberInviteStatisticsService memberInviteStatisticsService;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    @Autowired
    private LbbMemberInviteDetailMapper lbbMemberInviteDetailMapper;

    @Autowired
    private MemberNoticeService noticeService;
    @Autowired
    private MemberTradeService tradeService;
    @Autowired
    private MemberRebateTotalmoneyService memberRebateTotalmoneyService;
    @Autowired
    private DictService dictService;
    @Autowired
    private MemberInviteRebateRecordService memberInviteRebateRecordService;
    @Autowired
    private MemberInviteRebateRecordMapper memberInviteRebateRecordMapper;




    public LbbMemberInviteDetail getByInviteAndMemberId(String memberId, String inviteId) {
        LbbMemberInviteDetailExample example = new LbbMemberInviteDetailExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andInviteIdEqualTo(inviteId)
                .andHasDeletedEqualTo(Booleans.False.name());
        List<LbbMemberInviteDetail> list = lbbMemberInviteDetailMapper.selectByExample(example);
        if (list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public void insert(String memberId, String inviteId) {
        try {
            LbbMemberInviteDetail memberInviteDetail = new LbbMemberInviteDetail();
            memberInviteDetail.setCreateTime(new Date());
            memberInviteDetail.setEnable(Whether.YES.getDbIndex());
            memberInviteDetail.setHasChecked(1);
            memberInviteDetail.setInviteId(inviteId);
            memberInviteDetail.setMemberId(memberId);
            memberInviteDetail.setModifyTime(new Date());
            memberInviteDetail.setHasDeleted(Booleans.False.name());
            memberInviteDetail.setCreateTime(new Date());
            lbbMemberInviteDetailMapper.insertSelective(memberInviteDetail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public LbbMemberInviteDetail getByMemberId(String memberId) {
        return lbbMemberInviteDetailMapper.getByMemberId(memberId);
    }

    public void updateEnableById(String memberId, String inviterId) {
        lbbMemberInviteDetailMapper.updateEnableById(memberId, inviterId);
    }


    /**
     * 邀请绑定任务分发奖励和通知
     *
     * @param memberId
     * @param lbbMember
     * @return
     */
    public void rewordTask(String memberId, LbbMember lbbMember,RequestHeaders header) {
        LbbMember member = lbbMemberMapper.getMemberByMemberId(memberId);
        //查询当前用户是否有师傅返利金额累计表（如果没有就创建）
        memberRebateTotalmoneyService.selectById(member.getMemberId());
        LbbDict sfRebate = dictService.selectByKeyName("sf_rebate");
        //用户完成拉新任务，获得徒弟获得1元拉新红包
        String title1 = "恭喜拜师成功";
        String desc1 = "获得1元拜师奖励！";
        //tradeService.rebateMoney(memberId, 100, title1, desc1,3,header);
        //计入累计收益
        memberInviteStatisticsService.addTotalAmount(memberId,100,3);
        //师徒收益存入redis
//        memberInviteRebateRecordService.settoday(memberId,100);

        noticeService.addNoticeAndPush(title1, desc1, memberId, new RequestHeaders());
        Integer amount=Integer.parseInt(sfRebate.getKeyvalue());
        //用户完成拉新任务，师傅获得0.2元拉新红包
        String title2 = "恭喜收徒成功";
        String desc2 = "获得"+new BigDecimal(amount).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)+"元收徒奖励！";
        tradeService.addMoney(lbbMember.getMemberId(), amount,
                "徒弟赚钱返利",
                "徒弟赚钱返利",
                "", header,"", new MoneyCategory(Whether.YES, TaskCategory.MASTER).notMaster());
        tradeService.addMoney(memberId, 100,
                "徒弟拜师成功",
                "徒弟拜师成功",
                "", header,"", new MoneyCategory(Whether.YES, TaskCategory.MASTER).notMaster());
        noticeService.addNoticeAndPush(title2,desc2,lbbMember.getMemberId(),new RequestHeaders());

        //.一个徒弟/徒孙可给师傅带来的最高收益累计/此时id为等级最低的徒孙id
        memberRebateTotalmoneyService.addTotalMoney(memberId,0,amount);
        //计入累计收益
        memberInviteStatisticsService.addTotalAmount(lbbMember.getMemberId(),amount,0);
        //师徒收益存入redis
        memberInviteRebateRecordService.settoday(lbbMember.getMemberId(),amount);

        MemberInviteRebateRecord rebateRecord=new MemberInviteRebateRecord();
        rebateRecord.setCreateTime(LocalDateTime.now());
        rebateRecord.setMemberId(memberId);
        rebateRecord.setMoney(amount);
        rebateRecord.setType(0);
        rebateRecord.setInviteId(lbbMember.getMemberId());
        rebateRecord.setRateRebate(0);
        memberInviteRebateRecordMapper.insert(rebateRecord);

    }

    public List<String> getTdByMemberId(String memberId) {
        return lbbMemberInviteDetailMapper.getTdByMemberId(memberId);
    }

    /**
     * 检查是否收过徒弟
     * @param memberId 登录账号id
     * @return
     */
    public boolean checkBind(String memberId) {
            Integer check = lbbMemberInviteDetailMapper.checkTdByMemberId(memberId);
            if(check!=null){
                return false;
            }
        return true;
    }
}
